/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cocoserver.model.connectr;

import com.coco.message.model.LoginUser;
import com.coco.message.model.Message;
import com.coco.message.model.User;
import com.cocoserver.model.manager.ManageClientThread;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AIDEN
 */
public class ServerConn extends Thread {

    private ServerSocket ss;
    private ArrayList<User> allUser;

    public ServerConn() {
        try {
            allUser = new ArrayList<User>();
            allUser.add(new User("0001", "路飞", "123456"));
            allUser.add(new User("0002", "索隆", "123456"));
            allUser.add(new User("0003", "娜美", "123456"));
            allUser.add(new User("0004", "乌索普", "123456"));
            allUser.add(new User("0005", "山治", "123456"));
            allUser.add(new User("0006", "乔巴", "123456"));
            allUser.add(new User("0007", "罗宾", "123456"));
            allUser.add(new User("0008", "弗兰奇", "123456"));
            allUser.add(new User("0009", "布鲁克", "123456"));
            ss = new ServerSocket(10010);
        } catch (IOException ex) {
            Logger.getLogger(ServerConn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        System.out.println("IP:172.0.0.1\nPort:10010\n服务器开始运行");
        while (true) {
            try {
                Socket s = ss.accept();
                boolean isFind = false;
                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                User u = (User) ois.readObject();
                for (int i = 0; i < this.allUser.size(); i++) {
                    if ((u.getUID().equals(allUser.get(i).getUID())) && (u.getPsw().equals(allUser.get(i).getPsw()))) {
                        u=allUser.get(i);
                        LoginUser lUser = new LoginUser(u);
                        lUser.setFriends(allUser);
                        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                        oos.writeObject(lUser);

                        //为每个用户开一个线程
                        ClientThread cThread = new ClientThread(s,lUser.getLoginUser());
                        cThread.start();
                        ManageClientThread.addClientThread(lUser.getLoginUser().getUID(), cThread);
                        //oos.close();
                        isFind = true;
                        break;
                    }
                }
                if (!isFind) {
                    ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                    Message m = new Message();
                    m.setMsgType(Message.MSG_LOGIN_FAIL);
                    oos.writeObject(m);
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ServerConn.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ServerConn.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String getMsg(){
        return "IP:172.0.0.1\nPort:10010\n服务器开始运行";
    }
}
